package com.hx.springboot1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hx.springboot1.dao.ChooseDao;
import com.hx.springboot1.dao.ScoreDao;
import com.hx.springboot1.dao.StuDao;
import com.hx.springboot1.dao.UserDao;
import com.hx.springboot1.pojo.dto.StuDTO;
import com.hx.springboot1.pojo.entity.TabStudent;
import com.hx.springboot1.pojo.entity.TabUser;
import com.hx.springboot1.service.StuService;
import com.hx.springboot1.utils.PinYinUtils;
import com.hx.springboot1.utils.ResultMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Author: hx
 * Date: 2021-05-23 15/41
 * Description: Nothing else
 */
@Transactional
@Slf4j
@Service
public class StuServiceImpl implements StuService {
    @Autowired
    ChooseDao chooseDao;
    @Autowired
    ScoreDao scoreDao;
    @Autowired
    StuDao stuDao;
    @Autowired
    UserDao userDao;
    @Override
    public PageInfo selectAllStu(String searchName, String pageNum, String pageSize) {
        List<StuDTO> allStu;
        int pn = 1;
        int ps = 10;
        if (!StringUtils.isEmpty(pageNum)) {
            pn = Integer.parseInt(pageNum);
        }
        if (!StringUtils.isEmpty(pageSize)) {
            ps = Integer.parseInt(pageSize);
        }
        //   PageHelper  要在查询之前使用
        PageHelper.startPage(pn, ps);
        if (StringUtils.isEmpty(searchName)) {
            allStu = stuDao.selectAllStu();
            log.info("用户{}点击stuList页面", SecurityContextHolder.getContext().getAuthentication().getName());
        } else {
            searchName = "%" + searchName + "%";
            allStu = stuDao.selectAllStuByName(searchName);
            log.info("{}查询了学生：{}的信息", SecurityContextHolder.getContext().getAuthentication().getName(), searchName);
        }
        PageInfo<StuDTO> pageInfo = new PageInfo<>(allStu);
        return pageInfo;
    }

    @Override
    public ResultMsg insertOrUpdateStu(Integer stuId, Integer userId, Integer collegeId, Integer majorId, String stuName, String stuSex) {
        int effectRows = 0;
        if (stuId==null&&userId==null){
            /* 说明是新增 */
            // 先新增一个账号   是否首字母大写
            String name = PinYinUtils.getCamelPinYin(stuName, false);
            userDao.insertUser(name);
            TabUser login = userDao.login(name);
            effectRows += stuDao.insertStu(login.getUserId(), collegeId, majorId, stuName, stuSex);
        }else {
            effectRows += stuDao.updateStu(stuId, collegeId, majorId, stuName, stuSex);
        }
        if (effectRows>0){
            return new ResultMsg("操作成功！",true);
        }
        return new ResultMsg("操作失败！",false);
    }

    @Override
    public TabStudent selectStuById(Integer id) {
        if (id!=null){
            return stuDao.selectByStuId(id);
        }
        return null;
    }

    @Override
    public ResultMsg delStu(String SIDAndUID) {
        int result = 0;
        if (!StringUtils.isEmpty(SIDAndUID)) {
            String[] split = SIDAndUID.split("/");
            result += scoreDao.delScoreByStuId(Integer.parseInt(split[0]));
            result += chooseDao.delChooseByStuId(Integer.parseInt(split[0]));
            result += stuDao.delStu(Integer.parseInt(split[0]));
            userDao.updateUserState(1, Integer.parseInt(split[1]));
        }
        if (result > 0) {
            return new ResultMsg("删除成功！账户已锁定",true);
        }
        return new ResultMsg("删除失败！",false);
    }
}
